#coding:utf-8
__author__ = 'yasin'

from sqlalchemy import Column, String, Integer, Boolean, DateTime, UniqueConstraint, Text
from sqlalchemy.orm.exc import NoResultFound
from flask import Flask
from init_db import Base, db_session
#from flask.ext.sqlalchemy import SQLAlchemy

#app = Flask(__name__)
#app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:111111@localhost/myt5?charset=utf8'
#app.config['SQLALCHEMY_ECHO'] = True
#db = SQLAlchemy(app)
#Base = db.Model


class User(Base):
    __tablename__ = 'wg_user'
    USER_VIP = 1
    USER_NORMAL = 2
    USER_TEST = 3

    USER_MSO = 10
    USER_BOX = 11
    USER_MSO_VISITOR = 12
    id = Column(Integer, primary_key=True)
    username = Column(String(50))
    email = Column(String(50))
    passwd = Column(String(50))
    description = Column(String(200))
    super_user = Column(Boolean(), default=0)
    user_type = Column(Integer)
    reg_time = Column(DateTime)
    last_login_time = Column(DateTime)


    def __init__(self, name=None, email=None):
        self.username = name
        self.email = email

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def get_id(self):
        return unicode(self.id)


class Account(Base):
    ACCOUNT_NEW = 1
    ACCOUNT_ADD = 2
    ACCOUNT_ERR = 3
    __tablename__ = 'wg_account'
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer())
    account = Column(String(50))
    token = Column(String(40))
    static = Column(Integer)
    add_time = Column(DateTime)
    sub_message = Column(Integer)
    is_dev = Column(Boolean())
    status = Column(Integer)
    wei_home = Column(Integer)

    #name = Column(String(50))

class SubMessage(Base):
    __tablename__ = 'wg_sub_message'
    id = Column(Integer, primary_key=True)
    message = Column(Text)
    status = Column(Integer)

class WeiHome(Base):
    __tablename__ = 'wg_wei_home'
    id = Column(Integer, primary_key=True)
    title = Column(String(20))
    template_type = Column(Integer)
    template_type_name = Column(String(30))
    #template_content = Column(Text())





class Package(Base):
    __tablename__ = 'opda_package'
    id = Column(Integer, primary_key=True)
    name = Column(String(100))
    file_name = Column(String(150))
    package_name = Column(String(150))
    url = Column(String(50))
    uploader_id = Column(Integer())
    md5 = Column(String(50))
    update_time = Column(DateTime())
    size = Column(Integer())
    info = Column(String(200))

class Box(Base):
    __tablename__ = 'opda_box'
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer())
    mac = Column(String(50))
    cpu_id = Column(String(50))
    info = Column(String(200))
    last_upload = Column(DateTime())

class BoxPackage(Base):
    P_SYSTEM = 1
    P_DATA = 2
    __tablename__ = 'opda_box_package'
    id = Column(Integer, primary_key=True)
    box_id = Column(Integer())
    package_id = Column(Integer())
    position = Column(Integer())
    __table_args__ = (UniqueConstraint('box_id', 'package_id', name='box_package_map'),)
    
class BoxDevice(Base):
    __tablename__ = 'opda_box_device'
    id = Column(Integer, primary_key=True)
    box_id = Column(Integer())
    dv_release = Column(String(20))
    dv_imei = Column(String(20))
    dv_imei2 = Column(String(20))
    dv_vendor = Column(String(20))
    dv_sn = Column(String(20))
    dv_wifimac = Column(String(20))
    dv_brand = Column(String(20))
    dv_model = Column(String(20))
    dv_sdk = Column(Integer())
    upload_time = Column(DateTime())
    
    

class App(Base):
    __tablename__ = 'mso_app'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    visitor = Column(Integer)

    def visitor_name(self):
        try:
            return User.query.filter(User.id == self.visitor).one().username
        except NoResultFound:
            return u'未分配'

class AppActive(Base):
    __tablename__ = 'mso_app_active'
    id = Column(Integer, primary_key=True)
    app_id = Column(Integer())
    record_time = Column(DateTime()) #手动记录
    add_time = Column(DateTime())     #自动记录
    active_amount = Column(Integer()) #激活数



class Message(Base):
    TYPE_TEXT = 1
    TYPE_PIC = 2
    TYPE_VOICE = 3
    TYPE_VIDEO = 4
    TYPE_MUSIC = 5
    TYPE_NEW = 6
    TYPE_LOCATION = 7
    TYPE_LINK = 8
    __tablename__ = 'wg_message'
    id = Column(Integer, primary_key=True)
    to_user_name = Column(String(100))
    from_user_name = Column(String(100))
    create_time = Column(Integer)
    msg_type = Column(Integer)





if __name__ == "__main__":
    print Base.metadata
    pass
